﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using Yugi_Gui;
using Yugi.Client.DataModel;
using System.Threading;
using Common;

using System.Windows.Media;
namespace Yugi.Client.Control
{
	/// <summary>
	/// Interaction logic for YStateControl.xaml
    /// Quan ly la bai theo Toa Do Grid Row, Column
    /// 
	/// </summary>
	public partial class YTableControl : UserControl,IInvoke
	{
        TableModel model;
        
		public YTableControl()
		{
			this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(YTableControl_DataContextChanged);
           
		}

        void YTableControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!(e.NewValue is TableModel)) return;
            model = (TableModel)e.NewValue;
            model.Presenter = this;
            this.DataContext = (TableModel)e.NewValue;
            model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(model_PropertyChanged);
        }

        void model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "YStateNow")
            {
                switch (model.YStateNow)
                {
                    case EnumTable.MainPhase1:
                        
                        break;
                    case EnumTable.BattlePhase:
                        InitBattlePharse();
                        break;
                }
            }
        }



        
       
       
        //----------------------------MF1--------------------------------//
        //----------------------------BF--------------------------------//
        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (model.IsProcess)
                return;
            if (!is_battle_phase) return;
            if (!tan_cong_direct) return;
            if (card1 != null )
            {
               
                //Danh Dau Da Tan Cong Roi
                Data[card1.ID].CardControl.DaTanCong = true;
               


                model.YSupervisorModel.SendMessageToServer(Common.YMSG.CARD_TANCONG_TRUCTIEP, card1.ID);
                card1 = null;


              


            }

        }
        //------------------------------------------------------------//

        void GetPosition(out int x, out int y, int id1)
        {
            x = Grid.GetRow(Data[id1].Host);
            y = Grid.GetColumn(Data[id1].Host);
          
        }

        #region IInvoke Members

            public void Do(ClientMSG msg, params object[] obj)
            {
                switch (msg)
                {
                    case ClientMSG.LenBaiHopLe:
                        //Cho Chon Vi Tri
                        //allow_trace = true;
                        
                        
                        YBaseCard card = model.YSupervisorModel.Nguoi_Choi.Find((int)obj[0]);
                        queue.Push(card);
                        state.Push((Common.YMSG)obj[1]);
                        // Tu Dong Chon Vi Tri
                        LenBaiNormal();
                        break;
                    case ClientMSG.LenBaiKhongPhuHop:
                        MessageBox.Show(obj[0].ToString());
                        break;
                    case ClientMSG.ChoPhepDoiCongThu:
                        InitMenuDoiCongThu();
                        break;
                    case ClientMSG.LenBaiDoiThu:
                        LenBaiDoiThu(obj[0] as YBaseCard, (int)obj[1], (int)obj[2], (Common.YMSG)obj[3]);
                        break;
                    case ClientMSG.KetThucMF:
                        DelMenuDoiCongThu();
                        if(choice!=null)
                            choice.End();
                        break;
                    //---------------------Spell---------------------------//
                    case ClientMSG.LenBaiSpellHopLe:
                        card = model.YSupervisorModel.Nguoi_Choi.Find((int)obj[0]);
                        LenBaiMagic(card, (Common.YMSG)obj[1]);
                        break;
                    case ClientMSG.LenBaiSpellDoiThu:
                        LenBaiDoiThu(obj[0] as YBaseCard, (int)obj[1], (int)obj[2], (Common.YMSG)obj[3]);
                        break;
                    //---------------------Trap----------------------------//
                    case ClientMSG.LenBaiTrapHopLe:
                        card = model.YSupervisorModel.Nguoi_Choi.Find((int)obj[0]);
                        LenBaiMagic(card, (Common.YMSG)obj[1]);
                        break;
                    case ClientMSG.LenBaiTrapDoiThu:
                        LenBaiDoiThu(obj[0] as YBaseCard, (int)obj[1], (int)obj[2], (Common.YMSG)obj[3]);
                        break;
                    //---------------------BF-------------------------
                    case ClientMSG.KetThucBF:
                        DelBattlePharse();
                        break;
                   
                    case ClientMSG.DoiCongThuDoiThu:
                        DoiCongThuDoiThu((int)obj[0], (Common.YMSG)obj[1]);
                        break;
                    case ClientMSG.XoaBai:
                        XoaBai((int)obj[0]);
                        break;
                    case ClientMSG.DoiThuTanCong:
                        AnimationBiTanCong((int)obj[0], (int)obj[1]);
                        break;
                    case ClientMSG.DoiThuTanCongTrucTiep:
                        AniamationBiTanCongDirect((int)obj[0],(int)obj[1]);
                        break;
                    //---------------------Special-------------------------
                    case ClientMSG.LenBaiDacBiet:
                        YMonsterCard normal_card = obj[0] as YMonsterCard;
                        queue.Push(obj[0] as YBaseCard);
                        state.Push((Common.YMSG)obj[1]);
                        if(normal_card.Level<=Block.Block.SpecialSummon_Tribute_2Card_Star)
                            LenBaiDacBiet(1);
                        else
                            LenBaiDacBiet(2);
                        break;
                    case ClientMSG.LenBaiDacBietHopLe:
                        TheHienLenBaiDacBiet();
                        break;
                    case ClientMSG.LenBaiDacBietDoiThu:
                        List<int> ids = new List<int>();
                        int count = (int)obj[1];
                        for (int i = 2; i < count+2; i++)
                        {
                            ids.Add((int)obj[i]);
                        }
                        LenBaiDacBietDoiThu(obj[0] as YBaseCard, ids, (int)obj[count + 2], (int)obj[count + 3], (Common.YMSG)obj[count + 4]);
                        break;
                    case ClientMSG.XoaLenBaiDacBiet:
                        if(choice!=null)
                            choice.End();
                        break;
                    //------------------------API-------------------------//
                    case ClientMSG.ThayDoiCong:
                        this.ThayDoiChiSo((int)obj[0], (int)obj[1],true);
                        break;
                    case ClientMSG.ThayDoiThu:
                        this.ThayDoiChiSo((int)obj[0], (int)obj[1], false);
                        break;
                    case ClientMSG.Api_SpecialSummon:
                        this.SpecialSummon((int)obj[0],(YBaseCard)obj[1],(Zone)obj[2]);
                        break;
                    case ClientMSG.Api_SpecialSummon_DoiThu:
                        this.SpecialSummon_DoiThu((int)obj[0], (YBaseCard)obj[1], (int)obj[2], (int)obj[3], (Zone)obj[4], (YMSG)obj[5]);
                        break;
                    case ClientMSG.Api_ChangeControl:
                        this.ChangeControl((int)obj[0], (int)obj[1], (int)obj[2]);
                        break;
                    case ClientMSG.Api_MoveToHand:
                        this.MoveTohand((int)obj[0]);
                        break;
                    case ClientMSG.Api_MoveToHand_DoiThu:
                        this.MoveToHandDoiThu((int)obj[0]);
                        break;
                    //------------------------API Request----------------//
                    case ClientMSG.Request:
                        RequestAPI.ReqestItem item = RequestAPI.Get();
                        this.Process_Request(item);
                        break;
                    //-----------------------Active---------------------//
                    case ClientMSG.ShowListCanExcute:
                        ShowActive(obj);
                        break;
                    case ClientMSG.CloseActive:
                        CloseActive();
                        break;
                    case ClientMSG.Reset:
                        Reset();
                        break;
                }
            }
            public void Refresh()
            {
                
            }
        #endregion
        Stack<YBaseCard> queue = new Stack<YBaseCard>();
        Stack<Common.YMSG> state = new Stack<Common.YMSG>();
        Dictionary<int, HostItem> Data = new Dictionary<int, HostItem>();
        bool[,] ViTri = new bool[10,10];
        YBaseCard card1;
        YBaseCard card2;
        Common.YMSG state_card_doi_thu;
        Rectangle last;
        bool tan_cong_direct = false;
        bool is_battle_phase = false;
    }
   
}
 